﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dtos;
using Core.Domain;
using Core.Messages;
using Core.Mappers;
using AutoMapper;
using AutoMapper.Mappers;
using Core.Context;

namespace DI
{
    public static class AutoMapperConfig
    {
        private static bool s_isInit = false;

        public static void CreateMappings()
        {
            if (s_isInit)
                return;
            s_isInit = true;                                                    

            Mapper.CreateMap<Person, PersonDto>();
            Mapper.AddProfile<ListResponseProfile<Person, PersonDto>>();
            Mapper.CreateMap<PersonDto, Person>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());                      

            Mapper.CreateMap<MotorPolicy, MotorPolicyDto>()
                      .ForMember(x => x.ApprovalActions, opt => opt.MapFrom(origin => origin.ComputeApprovalActions()))
                      .ForMember(x => x.UserUserName, opt => opt.Ignore());
            Mapper.AddProfile<ListResponseProfile<MotorPolicy, MotorPolicyDto>>();
            Mapper.CreateMap<MotorPolicyDto, MotorPolicy>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Person, opt => opt.Ignore())
                      .ForMember(x => x.PersonID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.WFLogs, opt => opt.Ignore())                      
                      .ForMember(x => x.UserID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      //.ForMember(x => x.User, opt => opt.Ignore())
                      .ForMember(x => x.Workflow, opt => opt.Ignore());

            Mapper.CreateMap<Policy, PolicyDto>()
                      .Include<MotorPolicy, MotorPolicyDto>()
                      .ForMember(x => x.ApprovalActions, opt => opt.MapFrom(origin => origin.ComputeApprovalActions()))
                      .ForMember(x => x.UserUserName, opt => opt.Ignore());
            Mapper.AddProfile<ListResponseProfile<Policy, PolicyDto>>();
            Mapper.CreateMap<PolicyDto, Policy>()
                      .Include<MotorPolicyDto, MotorPolicy>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Person, opt => opt.Ignore())
                      .ForMember(x => x.PersonID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.StateID, opt => opt.Ignore())
                      .ForMember(x => x.WFLogs, opt => opt.Ignore())
                      .ForMember(x => x.UserID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      //.ForMember(x => x.User, opt => opt.Ignore())
                      .ForMember(x => x.Workflow, opt => opt.Ignore());                      

            Mapper.CreateMap<PolicyWFLog, PolicyWFLogDto>()
                      .ForMember(x => x.UserUserName, opt => opt.Ignore());
            Mapper.AddProfile<ListResponseProfile<PolicyWFLog, PolicyWFLogDto>>();
            Mapper.CreateMap<PolicyWFLogDto, PolicyWFLog>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.PolicyID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.StateID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.ActionID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.UserID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)));
                      //.ForMember(x => x.User, opt => opt.Ignore());                      

            Mapper.AssertConfigurationIsValid();            
        }
    }
}      